<?php
class Discuss extends Model
{
    const PROJECT = 2;
    
	const STATUS_INIT = 0;
	const STATUS_OK = 1;
	const STATUS_DEL = -1;
	
	const STATUS_REPLAY_INIT = 0;
	const STATUS_REPLAY_OK = 1;
	const STATUS_REPLAY_DEL = -1;
	
	public static $nodeIndex = array(); //树形回复的编号
	
    public static function queue($a)
    {
        FileLog::ini('queue')->info(json_encode($a));
    }
    
    /**
     * 获取某一个节点的第一层回复
     * where条件中限定了len值
     * @param $id
     * @param $rootId
     * @return array
     */
    public static function getNextLevelReplay($id, $rootId)
    {
        $info = self::link('replay')
            ->where(['rootid' => $rootId, 'id' => $id])
            ->getOne();
        
        $path = $info['path'];
        $len = $info['len'];
        
        $childs = self::link('replay')
            ->where(['rootid' => $rootId,'len' => $len+1])
            ->addWhere("path like '{$path}%'")
            ->order('id asc')
            ->getAll();
        
        return $childs;
        
    }
    
    /**
     * 获取某一个节点的所有回复
     * where条件中限定了len值
     * @param $id
     * @param $rootId
     * @return string
     */
    public static function getAllChild($id, $rootId)
    {
        $info = self::link('replay')
            ->where(['rootid' => $rootId, 'id' => $id])
            ->getOne();
        
        if (empty($info)) {
            return '';
        }
    
        $path = $info['path'];
        $len = $info['len'];
    
        $childs = self::link('replay')
            ->where(['rootid' => $rootId])
            ->whereGT('len', $len)
            ->addWhere("path like '{$path}%'")
            ->order('id asc')
            ->select()
            ->data()
            ->array_column('path', 'id')
            ->data;
        
        $childsTree = self::makeTree($childs);
    
        return json_encode($childsTree);
    }
    
    /**
     * 获取某一个话题的所有回复
     * @param $rootId
     * @return array
     */
    public static function getAllReplay($rootId)
    {
        $childs = self::link('replay')
            ->where(['rootid' => $rootId])
            ->order('len asc, id asc')
            ->select()
            ->getAll();
    
        return self::makeTree($childs);
        
    }
    
    /**
     * 构建子回复树
     * @param array $childs 子回复的信息 [['id' => 1234, 'content' => 'sssss', 'path' => '1,123,1234']]
     * @return array()
     */
    public static function makeTree($childs)
    {
        $tree = array();
        $tree[] = array('currentId' => 0, 'preId' => -1, 'name' => '静下心来, 慢慢吵'); //dtree.js 要求根元素的父id必须是-1, 从调试来看, 根元素的id必须是0才能正确高亮显示
        foreach ($childs as $replay) {
            $id = $replay['id'];
            $content = $replay['content'];
            $path = rtrim($replay['path'], ',');
        
            if (strpos($path, ',') === FALSE) { //只有一层, 肯是对话题的回复
                $tree[] = array('currentId' => self::nodeId($path), 'preId' => 0, 'name' => $content, 'id' => $id);
            } else {
                //前提是childs里的数据是根据path的长度顺序排列
                //最后一个逗号的位置
                $i = strrpos($path, ',');
                $prePath = substr($path, 0, $i);
                
                $preId = self::nodeId($prePath); //生成一个唯一的id
                $nodeId = self::nodeId($path);
                
                $tree[] = array('currentId' => $nodeId, 'preId' => $preId, 'name' => $content, 'id' => $id);
                
            }
        }
        
        return $tree;
    }
    
    /**
     * 获取当前节点的id, 没有则新加
     * @param string $path 评论的层级, 可能会很长
     * @return mixed
     */
    public static function nodeId($path)
    {
        $maxId = empty(self::$nodeIndex) ? 0 : max(self::$nodeIndex);//当前最大值
        
        //node的长度
        $node = md5($path);
        
        if (empty(self::$nodeIndex[$node])) {
            self::$nodeIndex[$node] = $maxId + 1;
        }
        
        return self::$nodeIndex[$node];
    }
}